/*
 * ======================================================================
 *
 *       Filename:  tools.c
 *
 *
 *        Created:  09/30/2011 10:39:36 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * =======================================================================
 */
#include "tools.h"

int     mystrlen(char       *str)
{
  int   cpt = 0;

  for (; str[cpt] != '\0'; cpt++)
    ;

  return (cpt);
}

int     mystrcmp(char       *str1,
                 char       *str2)
{
  int   i = 0;

  if (mystrlen(str1) != mystrlen(str2))
    return (0);
  else
  {
    while ((str1[i] == str2[i]) && (str1[i] != '\0'))
      i++;

    if (str1[i] == '\0')
      return (1);
    else
      return (0);
  }
}

char    *mystrcat(char      *str1,
                  char      *str2)
{
  char  *res = NULL;
  int   pos = 0;

  res = malloc(sizeof (char) * ((mystrlen(str1) + mystrlen(str2) + 1)));

  for (int i = 0; str1[i] != '\0'; i++)
  {
    res[pos] = str1[i];
    pos++;
  }

  for (int i = 0; str2[i] != '\0'; i++)
  {
    res[pos] = str2[i];
    pos++;
  }

  res[pos] = '\0';

  return (res);
}

char    *extract_end(char   *str)
{
  char  *end = NULL;
  int   len = 0;
  int   i = 0;

  len = mystrlen(str);
  len--;

  for (; ((len > 0) && (str[len] != '/')); len--)
    ;
  end = malloc(sizeof (char) * (mystrlen(str) - len + 1));

  if (str[len] == '/')
  {
    len++;
    for(; str[len] != '\0'; len++)
    {
      end[i] = str[len];
      i++;
    }
  }
  else
    return (str);
  end[i] = '\0';

  return (end);
}

int     my_atoi(char    *s)
{
  int   res = 0;

  for (int i = 0; s[i] != '\0'; i++)
  {
    res = res * 10;
    res = res + (s[i] - '0');
  }

  return (res);
}
